package ${package.Controller};


import com.baomidou.mybatisplus.core.metadata.IPage;
<#if superControllerClassPackage??>
    import ${superControllerClassPackage};
</#if>
import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.ApiOperation;


/**
*
* ${table.comment!} 前端控制器
*
*
* @author ${author}
* @since ${date}
*/
<#if restControllerStyle>
    @RestController
<#else>
    @Controller
</#if>
@RequestMapping(ApiPath.ROOT + "<#if package.ModuleName??>/${package.ModuleName}</#if><#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
    class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
    <#if superControllerClass??>
        public class ${table.controllerName} extends ${superControllerClass} {
    <#else>
        public class ${table.controllerName} {
    </#if>
    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;

    /**
    * 获取列表
    */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<${entity}> data) {

    LambdaQueryWrapperExt<${entity}> wraps = new LambdaQueryWrapperExt<>();
    wraps.eq(${entity}::getDelFlag, DelFlag.OK.getCode());
    data.buildSpecWrap(wraps);

    IPage<${entity}> page = ${table.entityPath}Service.page(data.buildPage(), wraps);

    return Result.ok(data.buildTableResult(page));
    }

    /**
    * 根据详细信息
    */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result getEntity(@PathVariable Long id) {
    return Result.ok(${table.entityPath}Service.getById(id));
    }


    /**
    * 保存
    */
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody ${entity} entity) {
    return toAjaxBoolean(${table.entityPath}Service.saveOrUpdate(entity));
    }

    /**
    * 删除
    */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {

    if (ArrayUtil.isEmpty(ids)) {
    return toAjaxBoolean(false);
    }

    List<${entity}> removeEntityList = Arrays.stream(ids)
    .map(id -> {
    ${entity} entity = new ${entity}().setId(id);
    entity.setDelFlag(DelFlag.DELETED.getCode());
    entity.setDeleteTime(new Date());
    return entity;
    }).collect(Collectors.toList());

    return toAjaxBoolean(${table.entityPath}Service.updateBatchById(removeEntityList));
    }



    }
</#if>
